#include "centerwidget.h"

#include "interface/login.h"
#include "interface/ServiceChoose.h"
#include "interface/ChooseCar.h"
#include "interface/requestorderwindow.h"
#include "interface/ConfirmCarInterface.h"
#include "interface/waitcarwindow.h"
#include "interface/moveonwindow.h"

#include <assert.h>

#include "QDebug"

#include <QtCore/QCoreApplication>

CCenterWidget::CCenterWidget(interface::IInterface *beginInterface)
{
    this->currentWidget = NULL;

    // set the first widget
    this->setWidget(beginInterface);
}

CCenterWidget::~CCenterWidget()
{
    if (this->currentWidget != NULL)
    {
        delete this->currentWidget;
        this->currentWidget = NULL;
    }
}

interface::IInterface* CCenterWidget::getCurrentWidget()
{
    return currentWidget;
}

void CCenterWidget::changeWidget(interface::InterfaceRequestEnum from, interface::InterfaceRequestEnum next, void *param)
{
    switch (next)
    {
    case interface::LOGIN:
    {
        break;
    }

    case interface::SERVICE_CHOOSE:
    {
        this->setWidget(new interface::CServiceChooseInterface() );
        break;
    }

    case interface::BOOK_CAR:
    {
        this->setWidget(new interface::CRequestOrderInterface() );
        break;
    }

    case interface::CHOOSE_CAR:
    {
        this->setWidget(new interface::CChooseCarInterface(0, (protocal::ClientBook*)param) );
        break;
    }

    case interface::CONFIRM_CAR:
    {
        this->setWidget(new interface::CConfirmCarInterface(0, (PCHOSEN_CAR_INFO)param) );
        break;
    }

    case interface::WAITING_FOR_CAR:
    {
        this->setWidget(new interface::CWaitCarWindow(0, (PORDER_INFO)param) );
        break;
    }

    case interface::CAR_DRIVING:
    {
        this->setWidget(new interface::CMoveOnWindow(0,(QString*)param));
        break;
    }

    default:
    {
        //error
        qDebug() << "we have not implement the function\n";
        //assert(0);
        break;
    }
    }

}

void CCenterWidget::setWidget(interface::IInterface *widget)
{
    assert(widget != NULL);

    if (this->currentWidget != NULL)
    {
        delete this->currentWidget;
        this->currentWidget = NULL;
    }
    this->currentWidget = widget;

    connect(this->currentWidget, SIGNAL(changeInterfaces(interface::InterfaceRequestEnum, interface::InterfaceRequestEnum, void*)),\
            this, SLOT(changeWidget(interface::InterfaceRequestEnum,interface::InterfaceRequestEnum,void*)));

    // tell the container to change the center widget
    emit changeCenterWidget();
}
